// set function parseTime,formatTime to filter
// export { parseTime, formatTime } from '@/utils';
import Utils from '@/utils';
/**
 * mapInit
 * @param {*} value
 * @param {*} map
 */
export function in18Select(value, map) {
  return map[value] || map['other'];
}

function pluralize(time, label) {
  if (time === 1) {
    return time + label;
  }
  return time + label + 's';
}

export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time);
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute');
  }
  if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour');
  }
  return pluralize(~~(between / 86400), ' day');
}

/* 数字 格式化 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1e18, symbol: 'E' },
    { value: 1e15, symbol: 'P' },
    { value: 1e12, symbol: 'T' },
    { value: 1e9, symbol: 'G' },
    { value: 1e6, symbol: 'M' },
    { value: 1e3, symbol: 'k' }
  ];
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol;
    }
  }
  return num.toString();
}

/**
 * 类目分割
 * @param {*} categoryName
 */

export function categoryName(cat) {
  if (!cat) {
    return '-';
  }
  if (cat.includes && cat.includes('--')) {
    const arr = cat.split('--');
    return arr[arr.length - 1];
  }
  return cat;
}

/**
 * 逗号分位
 * @param {*} num
 */

export function toThousandFilter(num) {
  if (num === null) {
    return '--';
  }
  return (num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','));
}

/**
 * 百分比
 * @param {*} value
 */
export function percent(value) {
  if (!value) {
    return '-';
  }
  if (isNaN(value)) {
    value = 0;
  }

  return Utils.toFixed(value * 100, 2) + '%';
}

/**
 * 万元定位
 * @param {*} value
 */
export function thousand(value) {
  if (value === null) {
    return '--';
  }

  if (isNaN(value)) {
    return 0;
  }

  if (Number(value) === 0) {
    return 0;
  }
  if (value < 100) {
    return Utils.divide(value, 10000, 4);
  }
  const tnum = Utils.divide(value, 10000, 2);
  return Utils.formatNumNormal(tnum);
}
/**
 * w 格式化
 */
export function tenThousand(value) {
  if (value === null) {
    return '--';
  }
  if (isNaN(value)) {
    return 0;
  }
  if (Number(value) < 10000) {
    return value;
  }
  const tnum = Utils.divide(value, 10000, 2);
  console.log('tnum', tnum);
  console.log(toThousandFilter(tnum));
  return toThousandFilter(tnum) + 'w';
}
